package com.supermap.search.utils;

import android.app.Activity;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.supermap.search.Url_name;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by wyw on 2017/3/20.
 */

public class SpUtils {

    //新增巡检记录已阅读的方法
    public static List<String> getXunJianJilu(Activity activity) {
        return stringToList(activity.getSharedPreferences(Url_name.SP, 0).getString("readfile", ""));
    }

    //把字符串分割成list集合
     public static void setXunJvianJilu(Activity activity,String read){
         List<String> list = getXunJianJilu(activity);
         List<String> listtwo;
         StringBuffer haveread =new StringBuffer();
         SharedPreferences sp = activity.getSharedPreferences(Url_name.SP, 0);
         if (list!=null){
             if (!list.contains(read)){
                 listtwo = new ArrayList<>(list);
                 listtwo.add(read);
                 for (int i = 0;i<listtwo.size();i++){
                     haveread.append(listtwo.get(i));
                     if (i!=listtwo.size()-1){
                         haveread.append("##");
                     }
                 }
                 sp.edit().putString("readfile",haveread.toString()).commit();
             }
         }
     }

    public static List<String> stringToList(String string){
        List<String> list = new ArrayList<>();
    if (!TextUtils.isEmpty(string)){
        //数据不为空时候
        String [] readbody = string.split("##");
        if (readbody.length>0&&readbody!=null){
         list = Arrays.asList(readbody);
        }
    }
        return list;
    }
}
